//
//  ImageContainerViewController.swift
//  swift_demo_weibo
//
//  Created by 杨兵 on 15/5/22.
//  Copyright (c) 2015年 bing. All rights reserved.
//  图片容器

import UIKit

class ImageContainerViewController: UICollectionViewController, ImageContainerCellDelegate{

    // 保存图片
    var imageList = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    // 获取图
    func addPicture(cell: ImageContainerCell) {
        println("添加图片")
        addpicfromphoto()
    }
    
    // 重图库获取照片
    func addpicfromphoto() {
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            println("不支持访问相册")
            return
        }
        
        // 2.选择照片,实例化选择器,然后modal
        let picker = UIImagePickerController()
        
        // 3.代理设置,通过代理获取照片
        picker.delegate = self
        
        // 4.指定类型
        picker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        
        // 5.指定用户是否可以编辑图片,上传头像的时候可以使用
        presentViewController(picker, animated: true, completion: nil)
    }
    
    
    // 移除图片
    func removePicture(cell: ImageContainerCell) {
        let index = collectionView?.indexPathForCell(cell)
        imageList.removeAtIndex(index!.item)
        collectionView?.reloadData()
    }
    

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        // 注意多一个按钮为了显示图片
        return imageList.count + 1
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("composeimgcell", forIndexPath: indexPath) as! ImageContainerCell
    
        cell.delegate = self
        
        // 判断是否是最后一个,添加一直
        if indexPath.item < imageList.count {
            cell.image = imageList[indexPath.item]
        } else {
            cell.image = nil
        }

        return cell
    }

}


// MARK: - UIImagePickerControllerDelegate, UINavigationControllerDelegate
extension ImageContainerViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

        // 注意此处可以使用缩放图片,缩放图片为320
        self.imageList.append(image.scaleImage(width: 320))
        
        // 重新加载数据
        collectionView?.reloadData()
        
        // 需要手动关闭控制器
        dismissViewControllerAnimated(true, completion: nil)
    }

}




// MARK: - 代理
protocol ImageContainerCellDelegate: NSObjectProtocol {
    
    // 点击添加图片
    func addPicture(cell: ImageContainerCell);
    
    // 移除图片
    func removePicture(cell: ImageContainerCell);
}

// 图片容器视图的cell
class ImageContainerCell: UICollectionViewCell {
    var image: UIImage? {
        didSet {
            if self.image == nil {
                self.removePictureBtn.hidden = true
                
                // 开启交互
                self.addPictureBtn.userInteractionEnabled = true
                self.addPictureBtn.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
            } else {
                self.removePictureBtn.hidden = false
                
                // 图片添加后,就不能在点击
                self.addPictureBtn.userInteractionEnabled = false
                self.addPictureBtn.setImage(image, forState: UIControlState.Normal)
            }
        }
    }
    
    // 数据回传代理
    var delegate:ImageContainerCellDelegate?
    
    @IBOutlet weak var addPictureBtn: UIButton!
    
    @IBOutlet weak var removePictureBtn: UIButton!
    
    
    // 打开图库,添加
    @IBAction func addPicture(sender: UIButton) {
        delegate?.addPicture(self)
    }
    
    
    // 删除
    @IBAction func removePicture(sender: UIButton) {
        delegate?.removePicture(self)
    }
    
    
    // 约束图片的填充模式
    override func awakeFromNib() {
        addPictureBtn.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    }
    
}